package com.tangsw.springbootdemo.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.math.BigDecimal;

public class BigDecimal4Deserializer extends JsonDeserializer<BigDecimal> {
    @Override
    public BigDecimal deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        try{
            if(jsonParser!=null&&jsonParser.getDecimalValue()!=null){
                return jsonParser.getDecimalValue().setScale(2, BigDecimal.ROUND_HALF_UP);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
